/* -*- indent-tabs-mode:T; c-basic-offset:8; tab-width:8; -*- vi: set ts=8:
 * $Id: byteorder.h,v 1.1 2004/09/12 03:40:38 poine Exp $
 * (c) 2003 Trammell Hudson <hudson@rotomotion.com>
 *
 * Byte ordering routines for the network layer.
 *
 *************
 *
 *  This file is part of the autopilot simulation package.
 *
 *  For more details:
 *
 *	http://autopilot.sourceforge.net/
 *
 *  Autopilot is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Autopilot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Autopilot; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifndef _byteorder_h_
#define _byteorder_h_


/*
 * Bring in ntohs, ntohl, htons, htonl from the BSD network
 * library.  Also bring in the byteswapping routines from the
 * GNU C library.
 */
#include <netinet/in.h>

#ifndef __APPLE__
#include <byteswap.h>
#endif


#ifdef __cplusplus
namespace util
{
#endif


/*
 * Convert floating point routines to i386 representation.  The
 * StrongARM and XScale seem to use a 32 bit word swapped version.
 * Luckilly, they both use little endian for the integer representations.
 */
static inline double
htonf(
	double			val
)
{
#ifndef __i386__
	uint32_t *		buf = (uint32_t*) &val;
	uint32_t		tmp = buf[0];
	buf[0] = buf[1];
	buf[1] = tmp;
#endif

	return val;
}


static inline double
ntohf(
	double			val
)
{
#ifndef __i386__
	return htonf( val );
#else
	return val;
#endif
}



/*
 * Byte swapping routines to convert to and from little
 * endian.  The u-blox GPS does not use network byte order,
 * instead it does everything as little endian.
 */
#ifdef CONFIG_BIG_ENDIAN
#	define ltohs( little )		bswap_16( little )
#	define htols( little )		bswap_16( little )
#	define ltohl( little )		bswap_32( little )
#	define htoll( little )		bswap_32( little )
#else
#	define ltohs( little )		(little)
#	define htols( little )		(little)
#	define ltohl( little )		(little)
#	define htoll( little )		(little)
#endif




#ifdef __cplusplus
}; /* namespace util */
#endif

#endif
